<%@ Page Culture="en-US" UICulture="en-US" Language="C#" Explicit="true" Debug="true"%>

<%@ OutputCache Location="None" VaryByParam="None" %>

<%@Import Namespace="System" %>
<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.SqlClient" %>
<%@Import Namespace="System.Data.OleDb" %>
<%@Import Namespace="System.Globalization"%>
<%@Import Namespace="System.Threading"%>



<Script LANGUAGE="C#" runat="server">

  //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  // To convert the SQL Server versions to MS Access do the following:-
  //   Replace the 2 DSN lines
  //   Replace all code of:-   SQLCommand   to   OleDbCommand
  //   Replace all code of:-   SQLDatareader   to   OleDbDatareader
  //   Replace Dates from '  to #  (do not need ' for SQL Server)  (eg strStartDate = "#" & strStartDate & "#")
  //   Change Function DateConvitfromformUS, also  (see comments in Function itself)
  //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

string strPK_ID;
string strPK_IDNEW;
string strarraysearch;
string strmachsearch;
string strSortField;
long intRowIDGet;
long intPageIDGet;
long intSkipGet;
long intPageSizeGet;


  //NET 1.1
  //OleDbConnection objConn = new OleDbConnection(ConfigurationSettings.AppSettings["DSN"]);
  //NET 2.0
  SqlConnection objConn = new SqlConnection(ConfigurationManager.AppSettings["DSN"]);
        
    //=======================================================================================
    // NOTE:-
    //Code that can be Customised are marked by &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    //
    //=======================================================================================


    void Page_Load() 
    {


        //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
        //Add Search criteria, or comment out if not using search criteria (can leave in)
        strarraysearch = Request.QueryString["textsearch"];
        if (strarraysearch == null
            || strarraysearch.Length == 0)
        {
            //nothing
        }
        else
        {
            strarraysearch = strarraysearch.Replace("'", "''");                     //so as to cope with any Apostrophes in text
        }

        strmachsearch = Request.QueryString["dropdownsearch"];
        if (strmachsearch == null
            || strmachsearch.Length == 0)
        {
            //nothing
        }
        else
        {
            strmachsearch = strmachsearch.Replace("'", "''");                     //so as to cope with any Apostrophes in text
        }
        //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

                //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
                //Search Criteria, if not used (Comment out next lines) or can just leave in as is.
                //strarraysearch = Request.QueryString["textsearch"]; 
                //strmachsearch = Request.QueryString["dropdownsearch"];  

                //strarraysearch = strarraysearch.Replace("'","''");    //so as to cope with any Apostrophes in text
                //strmachsearch = strmachsearch.Replace("'","''");    //so as to cope with any Apostrophes in text
                //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

                strPK_ID = Convert.ToString(Request.QueryString["num"]);     //passed PK_ID key
              
                //also get passed skipcount, pageid, rowid
                intRowIDGet = Convert.ToInt64(Request.QueryString["rowid"]);
                intPageIDGet = Convert.ToInt64(Request.QueryString["pageid"]);
                intSkipGet = Convert.ToInt64(Request.QueryString["skip"]);

                //get Sorted by field and PageSize, that are passed as well
                strSortField = Request.QueryString["sortby"];
                intPageSizeGet = Convert.ToInt64(Request.QueryString["pagesize"]);

                GetSkipCount();
                UpdateDBase();


                //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
                //Can takeout arraysearch and machsearch if not used in datagrid List
                 Response.Redirect("ajax_kgrid_list.aspx?action=GetCurrentPage" 
                                  +  "&num=" + Server.UrlEncode(strPK_IDNEW) 
                                  +  "&rowid=" + Server.UrlEncode(Convert.ToString(intRowIDGet))
                                  +  "&skip=" + Server.UrlEncode(Convert.ToString(intSkipGet)) 
                                  +  "&sortby=" + Server.UrlEncode(strSortField) 
                                  +  "&textsearch=" + Server.UrlEncode(strarraysearch) 
                                  +  "&dropdownsearch=" + Server.UrlEncode(strmachsearch)  
                                  +  "&pageid=" + Server.UrlEncode(Convert.ToString(intPageIDGet)));
                //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

    }



    void GetSkipCount() 
    {
        long intresult;
        long intremainder;
        long intRecordCount;
        int intwherecounter;
        string strSQLit;
        string strnotfound;
        string strPrevPKID;
        SqlCommand objCmd;
        SqlDataReader objRdr;
        
        //Response.Write("PKID is " + strPK_ID + "<br>" );

         //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
         //Change as required
         strSQLit = "SELECT PK_ID FROM ARRAYS2 ";                             //customise to your Table
         intwherecounter = 0;
         if (strarraysearch == null 
                    || (strarraysearch.Length == 0))
         {
            // nothing to Search
         }
         else 
         {
            // NOTE:- To make faster, put % only at end
            strSQLit = strSQLit + " WHERE Array_Name like '%" 
                        + strarraysearch + "%'";
            intwherecounter = 1;
         }

         if (strmachsearch == null
                    || (strmachsearch.Length == 0))
         {
            // nothing to Search
         }
         else if ((intwherecounter == 1)) 
         {
            strSQLit = strSQLit + " AND Loaded_Machine = '" 
                        + strmachsearch + "'";
         }
         else 
         {
            strSQLit = strSQLit + " WHERE Loaded_Machine = '" 
                        + strmachsearch + "'";
         }

         strSQLit = strSQLit + " ORDER BY " + strSortField;                    //customise to your Table
         //strSQLit = strSQLit + " ORDER BY PK_ID";                             //customise to your Table
         //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

         //Response.Write("strSQLit is " + strSQLit + "<br>");

         objCmd = new SqlCommand(strSQLit, objConn);
         objConn.Open();
         objRdr = objCmd.ExecuteReader();

         intRecordCount = 0;
         strnotfound = "Yes";
         strPrevPKID = "";

         while (objRdr.Read())
         {
          intRecordCount = intRecordCount + 1;                                  //&&&&&&&&&&&&&&&&&&&&&&&&&&

          if (Convert.ToString(objRdr["PK_ID"]) == strPK_ID)                 //customise to the key in your Table
          {
             if (intRecordCount == 1)            //at first record
             {
              objRdr.Close();
              objConn.Close();
              strnotfound = "No";
              GetSecondRecord();                //will now get second record after first, to use to highlight now
              return;   //like exit sub
             }
           
             if (intRecordCount > 1)            //if Not at first record
             {
              intRecordCount = intRecordCount - 1;
              strPK_IDNEW = strPrevPKID;  
             }
 
             break;

          }    //End (intRecordCount == 1)  


           strPrevPKID = Convert.ToString(objRdr["PK_ID"]);        //Change to suit Primary Key

         }     //End while



         objRdr.Close();
         objConn.Close();

        intresult = intRecordCount / intPageSizeGet;
        intremainder = intRecordCount % intPageSizeGet;

       //Response.Write("intresult  is " + intresult  + "<br>" );

        if (intremainder == 0) 
        {
          intRowIDGet = (intPageSizeGet - 1);                                 //must always take 1 off pageid (ie because row 1 is now row 0, for AJAX grid list)
          if (intRowIDGet < 0)                                //check tha rowid is not < 0
          {
            intRowIDGet = 0;
          }
          intPageIDGet = intresult;
          intSkipGet = (intresult * intPageSizeGet) - intPageSizeGet;
        }

        if (intremainder > 0) 
        {
          intRowIDGet = (intremainder - 1);                                      //must always take 1 off pageid (ie because row 1 is now row 0, for AJAX grid list)
          if (intRowIDGet < 0)                              //check tha rowid is not < 0
          {
            intRowIDGet = 0;
          }
          intPageIDGet = intresult + 1;
          intSkipGet = ((intresult + 1) * intPageSizeGet) - intPageSizeGet;
        }

    }


    void GetSecondRecord()
    {
        long intRecordCount;
        int intwherecounter;
        string strSQLit;
        string strnotfound;
        SqlCommand objCmd;
        SqlDataReader objRdr;
        
        //Response.Write("PKID is " + strPK_ID + "<br>" );

         //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
         //Change as required
         strSQLit = "SELECT PK_ID FROM ARRAYS2 ";                             //customise to your Table
         intwherecounter = 0;
         if (strarraysearch == null 
                    || (strarraysearch.Length == 0))
         {
            // nothing to Search
         }
         else 
         {
            // NOTE:- To make faster, put % only at end
            strSQLit = strSQLit + " WHERE Array_Name like '%" 
                        + strarraysearch + "%'";
            intwherecounter = 1;
         }

         if (strmachsearch == null
                    || (strmachsearch.Length == 0))
         {
            // nothing to Search
         }
         else if ((intwherecounter == 1)) 
         {
            strSQLit = strSQLit + " AND Loaded_Machine = '" 
                        + strmachsearch + "'";
         }
         else 
         {
            strSQLit = strSQLit + " WHERE Loaded_Machine = '" 
                        + strmachsearch + "'";
         }

         strSQLit = strSQLit + " ORDER BY " + strSortField;                    //customise to your Table
         //strSQLit = strSQLit + " ORDER BY PK_ID";                             //customise to your Table
         //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

         //Response.Write("strSQLit is " + strSQLit + "<br>");

         objCmd = new SqlCommand(strSQLit, objConn);
         objConn.Open();
         objRdr = objCmd.ExecuteReader();

         intRecordCount = 0;
         strnotfound = "Yes";

         while (objRdr.Read())
         {
             intRecordCount = intRecordCount + 1;                                  //&&&&&&&&&&&&&&&&&&&&&&&&&&

             strPK_IDNEW = Convert.ToString(objRdr["PK_ID"]);        //Change to suit Primary Key
         
             if (intRecordCount == 2)            //at next record after first record
             {
              break;   //exit while
             }
           
         }     //End while

         objRdr.Close();
         objConn.Close();

          //always set parameters at first record on first page
          intRowIDGet = 0;
          intPageIDGet = 1;
          intSkipGet = 0;

    }



    //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    //Change as required
    //------------------------------------------------
    void UpdateDBase()
    {
        string strSQL;
        SqlCommand objCmd;

        strSQL = "DELETE FROM ARRAYS2 WHERE PK_ID = " + strPK_ID + "";

        objCmd = new SqlCommand(strSQL, objConn);

        objConn.Open();
        objCmd.ExecuteNonQuery();
        objConn.Close();

    }
     //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&


</Script>


<HTML>
<HEAD>

</HEAD>
<BODY>

<form id="form1" method="post" runat="server">

</form>

</BODY>
</HTML>

 